import { CollectionRecordModel } from '@/models/collectionRecord'
import { CollectionRecordVO, getCollectionRecordPage, removeCollectionRecord } from '@/services/CollectionRecordService'
import { useModel } from '@@/exports'
import { ProColumns, ProTable } from '@ant-design/pro-components'
import { Button, Popconfirm, Space } from 'antd'

export interface CollectionRecorProps {
  tableRef?: any
}

const CollectionRecorTable = (props: CollectionRecorProps) => {
  // const access = useAccess()
  let { actions: notifyActions } = CollectionRecordModel

  let { getDictData, getDictEnum } = useModel('dict')

  /**
   * 列定义
   */
  const columns: ProColumns<CollectionRecordVO>[] = [
    {
      title: 'id',
      dataIndex: 'id',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '奖品id',
      dataIndex: 'activityAwardId',
      width: 20,
      hideInSearch: true,
    },
    {
      title: '奖品名',
      dataIndex: 'activityAwardName',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '活动id',
      dataIndex: 'activityId',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '活动名称',
      dataIndex: 'activityName',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '小程序appId',
      dataIndex: 'maAppId',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '小程序openId',
      dataIndex: 'maOpenId',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '小程序用户id',
      dataIndex: 'maUserId',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '公众号appId',
      dataIndex: 'mpAppId',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '公众号openId',
      dataIndex: 'mpOpenId',
      width: 120,
      hideInSearch: true,
    },
    {
      title: '操作',
      render: (_: any, record: CollectionRecordVO) => [
        <Space key="operate">
          {
            <Button type="link" size="small" onClick={() => notifyActions.openUpdateModal(true, record)}>
              编辑
            </Button>
          }
          {
            <Popconfirm
              title="确定要删除吗"
              onConfirm={() => {
                removeCollectionRecord([record.id]).then(() => props.tableRef?.current?.reload())
              }}
              okText="确认"
              cancelText="取消"
            >
              <Button type="link" danger size="small">
                删除
              </Button>
            </Popconfirm>
          }
        </Space>,
      ],
      hideInSearch: true,
    },
  ]

  // 工具栏
  let actions = [
    <Button key="create" type="primary" onClick={() => notifyActions.openUpdateModal(true, undefined)}>
      添加
    </Button>,
  ]

  return (
    <>
      <ProTable
        actionRef={props.tableRef}
        columns={columns}
        request={getCollectionRecordPage}
        toolbar={{
          actions: actions,
        }}
        scroll={{ x: 500 }}
        sticky={{ offsetHeader: 55, offsetScroll: 5 }}
      ></ProTable>
    </>
  )
}

export default CollectionRecorTable
